package com.mohe.zhihui.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mohe.zhihui.model.Project;
import com.mohe.zhihui.service.interfaces.IProject;
import com.mohe.zhihui.utils.Json;
import com.mohe.zhihui.utils.JsonUtil;
import com.mohe.zhihui.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

public class ProjectAction extends ActionSupport {

	private static final long serialVersionUID = -9125625211127572692L;
	
//	ApplicationContext ac;
	
	private IProject projectServ;
	
	private List<Project> prjList;
	private Project prj = new Project();
	private Page page = new Page();
	
	public IProject getProjectServ() {
		return projectServ;
	}

	public void setProjectServ(IProject projectServ) {
		this.projectServ = projectServ;
	}

	public List<Project> getPrjList() {
		return prjList;
	}

	public void setPrjList(List<Project> prjList) {
		this.prjList = prjList;
	}

	public Project getPrj() {
		return prj;
	}

	public void setPrj(Project prj) {
		this.prj = prj;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	
//	public ProjectAction() {
//		super();
//		ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//		projectServ = (IProject) ac.getBean("IProject");
//	}

	public String getProjectListForPage() {
		int count = projectServ.getCount(prj);
		count = 8;
		page.setCount(count);
		prjList = projectServ.getProjectList(prj, page);
		return "prjList.jsp";
	}

	public void searchProjectList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = projectServ.getCount(prj);
		page.setCount(count);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Project> list = projectServ.getProjectList(prj, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (Project s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getTitle())) {
					map.put("title", s.getTitle());
				} else {
					map.put("title", "");
				}
				if (s.getCreateDate() != null) {
					map.put("date", df.format(s.getCreateDate()));
				} else {
					map.put("date", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}

	/**
	 * 保存项目信息
	 */
	public void saveProject() {
		Json json = new Json();
		boolean result = projectServ.saveProject(prj);
		if (result) {
			json.setMessage("添加成功");
		} else {
			json.setSuccess(false);
			json.setMessage("添加失败");
		}
		JsonUtil.writeJson(json);
	}

	public String toEditProject() {
		prj = projectServ.getProjectById(prj.getTimeKey());
		return "updatePrj.jsp";
	}

	public void updateProject() {
//		Json json = new Json();
//		Project tmp = projectServ.getProjectById(prj.getTimeKey());
//		if (tmp != null) {
//			tmp.setTitle(prj.getTitle());
//			tmp.setContent(prj.getContent());
//			tmp.setStatus(prj.getStatus());
//			tmp.setCreateDate(new Date());
//			boolean result = projectServ.updateProject(tmp); // 有问题
//			if (result) {
//				json.setMessage("编辑成功");
//				prj = tmp;
//			} else {
//				json.setSuccess(true);
//				json.setMessage("编辑失败");
//			}
//		} else {
//			json.setSuccess(true);
//			json.setMessage("编辑失败");
//		}
//		JsonUtil.writeJson(json);
		
		Json json = new Json();
		boolean result = projectServ.updateProject(prj);
		if (result) {
			json.setMessage("编辑成功");
		} else {
			json.setSuccess(true);
			json.setMessage("编辑失败");
		}
		JsonUtil.writeJson(json);
	}

	public void delProject() {
		Json json = new Json();
		boolean result = projectServ.delProject(prj);
		if (result) {
			json.setMessage("删除成功");
		} else {
			json.setSuccess(false);
			json.setSuccess(false);
		}
		JsonUtil.writeJson(json);
	}

}
